﻿using System;
using DevExpress.XtraReports.UI;
using System.Data.SqlClient;
//using prjKQXS_UI.Utils;
using System.Data;
//using prjKQXS_UI.Dal;
using prjKQXS_DLL;

namespace prjKQXS_Report
{
    public partial class rptDSDaiLyTheChap_1Ky : DevExpress.XtraReports.UI.XtraReport
    {
        Report_DAL report_dal = new Report_DAL();

        public rptDSDaiLyTheChap_1Ky(DateTime FromDate, DateTime ToDate, bool DaRutTC=true)
        {

            // False là đại lý cấp 1
            // True là đại lý cấp 2
            InitializeComponent();

            dsReports1 = report_dal.reportDungChung(dsReports1);

            try
            {
                SqlConnection conn = new SqlConnection(BienTc.connString);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                string DieuKien_DaRutTC = "";
                if (!DaRutTC) DieuKien_DaRutTC = " and (Thanh_Ly=1 and isDaRut=1) ";

                    string sql = "SELECT HOPDONG.Ma_DaiLy, Ten_DaiLy, SoPhieuThu AS So_CT, Noi_O, So_Tien AS Goc, LAIXUAT_DL.NgayBD AS Ngay_TinhLai, NgayRutTC AS Ngay_Rut " +
                        "FROM HOPDONG INNER JOIN LAIXUAT_DL ON HOPDONG.Ma_DaiLy = LAIXUAT_DL.Ma_DaiLy " +
                        "WHERE So_Tien > 0 and (CAST(CONVERT(nvarchar, LAIXUAT_DL.NgayBD, 112) AS int) >= @BeginDate) " +
                        "and (CAST(CONVERT(nvarchar, LAIXUAT_DL.NgayRutTC, 112) AS int) <= @EndDate)" + DieuKien_DaRutTC +
                        "ORDER BY Ten_DaiLy";

                    //Chuyển ngày tháng thành dạng số (dùng cho việc so sánh ngày tháng trong lệnh SQL ở trên)
                    int begin = Convert.ToInt32(FromDate.ToString("yyyyMMdd"));
                    int end = Convert.ToInt32(ToDate.ToString("yyyyMMdd"));

                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.SelectCommand.Parameters.Clear();
                    da.SelectCommand.Parameters.AddWithValue("@BeginDate", begin);
                    da.SelectCommand.Parameters.AddWithValue("@EndDate", end);

                da.Fill(dsReports1.rptDSDaiLyTheChap);
            }
            catch (Exception e)
            {
                throw;
            }
        }

        int stt = 0;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            stt++;
            lblSTt.Text = stt.ToString();
        }
    }
}
